package com.egao.common.manage.student;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentSignBlank;
import com.egao.common.module.student.entity.vo.StudentSignBlankListVO;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.student.service.StudentSignBlankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 学生空白协议管理
 * @author lym
 */
@Api(tags = "学生空白协议管理")
@RestController
@RequestMapping("${api}")
public class StudentSignBlankController extends BaseController {

    @Autowired
    private StudentSignBlankService studentSignBlankService;
    @Autowired
    private StudentService studentService;

    private final String title = "学生空白协议管理";

    @PreAuthorize("hasAuthority('student')")
    @OperLog(value = title, desc = "分页查询空白协议")
    @ApiOperation(value = "分页查询空白协议")
    @ApiPageParam
    @GetMapping("/student/blank/page")
    public PageResult<StudentSignBlankListVO> page(HttpServletRequest request) {
        Student student = studentService.getByUserId(getLoginUserId());
        if (null == student) {
            throw new BusinessException("没有找到您的学生信息");
        }

        PageParam<StudentSignBlankListVO> pageParam = new PageParam<>(request);
        pageParam.put("studentId", student.getId());

        return studentSignBlankService.listPage(pageParam);
    }

    @PreAuthorize("hasAuthority('student')")
    @OperLog(value = title, desc = "空白协议打印表")
    @ApiPageParam
    @PostMapping("/student/blank/print")
    public JsonResult print(@RequestBody StudentSignBlank studentSignBlank) {
        return studentSignBlankService.print(studentSignBlank.getId(), getLoginUserId());
    }

    @PreAuthorize("hasAuthority('student')")
    @OperLog(value = title, desc = "上传打印表", result = true)
    @ApiOperation(value = "上传打印表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSignBlank", value = "studentSignBlank", required = true, dataType = "studentSignBlank")
    })
    @PostMapping("/student/blank/upload")
    public JsonResult submit(@RequestBody StudentSignBlank studentSignBlank) {
        return studentSignBlankService.upload(studentSignBlank, getLoginUserId());
    }

}
